package com.neusoft.neuiotms.dcom.converter;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcom.dto.QuestionnaireFeedbackDTO;
import com.neusoft.neuiotms.dcom.model.QuestionnaireFeedback;
import com.neusoft.neuiotms.dcom.repository.QuestionnaireFeedbackRepository;
import com.neusoft.neuiotms.dcom.repository.QuestionnaireRepository;

@Component
public class QuestionnaireFeedbackConverter extends AbstractConverter<QuestionnaireFeedback, QuestionnaireFeedbackDTO> {

    @Autowired
    private QuestionnaireFeedbackRepository questionnaireFeedbackRepository;
    @Autowired
    private QuestionnaireRepository questionnaireRepository;
    @Autowired
    private QuestionnaireConverter questionnaireConverter;

    @Override
    public QuestionnaireFeedback toModel(final QuestionnaireFeedbackDTO dto) {

        QuestionnaireFeedback model;

        if (dto.isNew()) {
            model = this.questionnaireFeedbackRepository.findByUsernameAndQuestionnaire_Id(dto.getUsername(),
                    dto.getQuestionnaireId());
            if (null == model) {
                model = new QuestionnaireFeedback();
                model.setUsername(dto.getUsername());
                model.setUser(dto.getUser());
                model.setQuestionnaire(this.questionnaireRepository.getOne(dto.getQuestionnaireId()));
                model.setCreateTime(new Date());
            }
        } else {
            model = this.questionnaireFeedbackRepository.getOne(dto.getId());
        }
        model.setInstitutionCode(dto.getInstitutionCode());
        model.setInstitutionName(dto.getInstitutionName());
        model.setGroupCode(dto.getGroupCode());
        model.setGroupName(dto.getGroupName());
        model.setPreface(dto.getPreface());
        model.setStatsRoutineWork(dto.getStatsRoutineWork());
        model.setStatsEnvWork(dto.getStatsEnvWork());
        model.setStatsNetworkWork(dto.getStatsNetworkWork());
        model.setStatsSecurityWork(dto.getStatsSecurityWork());
        model.setStatsServerWork(dto.getStatsServerWork());
        model.setStatsStorageWork(dto.getStatsStorageWork());
        model.setStatsAssetWork(dto.getStatsAssetWork());
        model.setStatsDocumentWork(dto.getStatsDocumentWork());
        model.setStatsInsuranceWork(dto.getStatsInsuranceWork());
        model.setStatsAssistWork(dto.getStatsAssistWork());
        model.setStatsIncrementWork(dto.getStatsIncrementWork());
        model.setExcellence(dto.getExcellence());
        model.setExperience(dto.getExperience());
        model.setTarget(dto.getTarget());
        model.setContent(dto.getContent());
        model.setUpdateTime(new Date());

        return model;
    }

    @Override
    public QuestionnaireFeedbackDTO toDTO(final QuestionnaireFeedback model, final boolean forListView) {

        final QuestionnaireFeedbackDTO dto = new QuestionnaireFeedbackDTO();

        dto.setId(model.getId());
        dto.setUsername(model.getUsername());
        dto.setUser(model.getUser());
        dto.setCreateTime(model.getCreateTime());
        dto.setUpdateTime(model.getUpdateTime());
        dto.setInstitutionCode(model.getInstitutionCode());
        dto.setInstitutionName(model.getInstitutionName());
        dto.setGroupCode(model.getGroupCode());
        dto.setGroupName(model.getGroupName());
        dto.setPreface(model.getPreface());
        dto.setStatsRoutineWork(model.getStatsRoutineWork());
        dto.setStatsEnvWork(model.getStatsEnvWork());
        dto.setStatsNetworkWork(model.getStatsNetworkWork());
        dto.setStatsSecurityWork(model.getStatsSecurityWork());
        dto.setStatsServerWork(model.getStatsServerWork());
        dto.setStatsStorageWork(model.getStatsStorageWork());
        dto.setStatsAssetWork(model.getStatsAssetWork());
        dto.setStatsDocumentWork(model.getStatsDocumentWork());
        dto.setStatsInsuranceWork(model.getStatsInsuranceWork());
        dto.setStatsAssistWork(model.getStatsAssistWork());
        dto.setStatsIncrementWork(model.getStatsIncrementWork());
        dto.setExcellence(model.getExcellence());
        dto.setExperience(model.getExperience());
        dto.setTarget(model.getTarget());
        dto.setContent(model.getContent());
        dto.setSubmitted(model.isSubmitted());
        dto.setSubmitAt(model.getSubmitAt());
        dto.setQuestionnaire(this.questionnaireConverter.toDTO(model.getQuestionnaire(), true));
        dto.setQuestionnaireId(model.getQuestionnaire().getId());

        return dto;
    }

}
